package o;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.appmarket.service.alarm.control.RepeatingTaskManager;
import com.huawei.appmarket.service.predownload.thread.PreDownloadManagerThread;
import com.huawei.appmarket.service.predownload.thread.PreDownloadManagerThread.ApkUpgradeInfoComparator;
import com.huawei.appmarket.service.predownload.thread.PreDownloadManagerThread.e;
import com.huawei.secure.android.common.intent.SafeBundle;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import o.dkb.d;

public class dqn extends dpn<Boolean, Boolean> implements e {
    private static boolean ˎ = false;
    private int ˋ;
    private final List<ApkUpgradeInfo> ˏ;

    protected /* synthetic */ Object ˋ(Context context, Object obj) throws InterruptedException {
        return ˎ(context, (Boolean) obj);
    }

    protected /* synthetic */ void ˏ(Context context, Object obj, Object obj2) throws InterruptedException {
        ˋ(context, (Boolean) obj, (Boolean) obj2);
    }

    protected /* synthetic */ Object ॱ(Context context) throws InterruptedException {
        return ˋ(context);
    }

    public dqn() {
        this.ˏ = new ArrayList();
        this.ˋ = -1;
        this.ˊ = "PreDlManagerTask";
    }

    public static boolean ᐝ() {
        return ˎ;
    }

    private static void ˋ(boolean z) {
        ˎ = z;
    }

    private boolean ʽ() {
        try {
            this.ˏ.clear();
            this.ˏ.addAll(dss.ॱ().ˎ(true, 1));
            return true;
        } catch (UnsupportedOperationException e) {
            Logger.e(this.ˊ, "addAll(UpdateManagerWrapper.create().getUpdateApps()) failed!" + e.toString());
            return false;
        }
    }

    protected Boolean ˋ(Context context) {
        ˋ(true);
        SafeBundle ˊ = ˊ();
        if (ˊ != null) {
            this.ˋ = ˊ.getInt(RepeatingTaskManager.KEY_PRE_DOWNLOAD_START_TYPE, -1);
        }
        if (-1 != this.ˋ) {
            ((edx) cdb.ॱ(edx.class)).ˋ(this.ˋ);
        }
        if (!dss.ॱ().ˎ()) {
            Logger.ˎ(this.ˊ, "no need preDownload, isOpenAutoUpdate is false");
            return Boolean.valueOf(false);
        } else if (!cep.ˏ()) {
            Logger.ˋ(this.ˊ, "auto update state is fault.");
            dss.ॱ().ˋ(false);
            return Boolean.valueOf(false);
        } else if (!ʽ()) {
            return Boolean.valueOf(false);
        } else {
            if (this.ˏ.isEmpty()) {
                Logger.ˎ(this.ˊ, "no need preDownload, no update");
                return Boolean.valueOf(false);
            } else if (!ˏ(context)) {
                return Boolean.valueOf(false);
            } else {
                if (!bmt.ˊ()) {
                    Logger.ˎ(this.ˊ, "no need preDownload, network is not reachable");
                    PreDownloadManagerThread.ˋ("netUnreachable");
                    return Boolean.valueOf(false);
                } else if (!edl.ˏ().ˏॱ() || System.currentTimeMillis() - edn.ˋ().ˏ() >= 180000) {
                    return Boolean.valueOf(true);
                } else {
                    Logger.ˎ(this.ˊ, "no need preDownload, can not start repeatedly in 3 minutes");
                    PreDownloadManagerThread.ˋ("prohibitTask");
                    return Boolean.valueOf(false);
                }
            }
        }
    }

    private boolean ˏ(Context context) {
        if (doo.ˊ(context) && doo.ॱॱ(context)) {
            int ˊ = edp.ˊ();
            if (ˊ > edl.ˏ().ˎ()) {
                Logger.ˎ(this.ˊ, "no need preDownload, cpu usage is too high: " + ˊ);
                PreDownloadManagerThread.ˋ("highCpu#" + ˊ);
                return false;
            }
            edj ˋ = edh.ˋ(context);
            if (ˋ.ॱ || ˋ.ˎ > edl.ˏ().ˊ()) {
                long ˏ = dss.ॱ().ˏ();
                long currentTimeMillis = System.currentTimeMillis();
                if (ˏ == 0 || 1500000 + ˏ < currentTimeMillis) {
                    ˊ = dug.ˎ(false);
                    if (ˊ != 0) {
                        cfw.ˊ(ˊ);
                        PreDownloadManagerThread.ˋ("checkServerFailed#" + ˊ);
                        Logger.ˎ(this.ˊ, "no need preDownload,can not get online update,last update:" + ˏ);
                        return false;
                    } else if (!ʽ()) {
                        return false;
                    } else {
                        if (this.ˏ.isEmpty()) {
                            Logger.ˎ(this.ˊ, "can not found update data.");
                            return false;
                        }
                    }
                }
                return true;
            }
            PreDownloadManagerThread.ˋ("lowBattery#" + ˋ.ॱ + "#" + ˋ.ˎ);
            Logger.ˎ(this.ˊ, "no need preDownload,battery status:" + ˋ.toString());
            return false;
        }
        Logger.ˎ(this.ˊ, "no network or network is not wifi");
        return false;
    }

    protected Boolean ˎ(Context context, Boolean bool) {
        if (bool == null || !bool.booleanValue()) {
            return Boolean.valueOf(false);
        }
        Logger.ˎ("ScheduleRepeatService", this.ˊ + " execute:" + this.ˏ.size());
        new PreDownloadManagerThread(context, this.ˋ, this.ˏ, this).ॱ();
        return Boolean.valueOf(true);
    }

    protected void ˋ(Context context, Boolean bool, Boolean bool2) {
        ˋ(false);
    }

    public void ˏ() {
        Logger.ˎ(this.ˊ, "start update dl, startType: " + this.ˋ);
    }

    public int ॱ() {
        return 1;
    }

    public Comparator<ApkUpgradeInfo> ˋ() {
        return new ApkUpgradeInfoComparator();
    }

    public void ˊ(@Nullable SessionDownloadTask sessionDownloadTask, @NonNull ApkUpgradeInfo apkUpgradeInfo, d dVar) {
        int ˋ = ((cdg) cdb.ॱ(cdg.class)).ˋ(apkUpgradeInfo.ᐝ());
        Logger.ˎ("PreDlManagerTask", "buildDownloadTask " + apkUpgradeInfo.ᐝ() + ",appStatus=" + ˋ);
        if (3 == ˋ || 4 == ˋ) {
            edw.ॱ(apkUpgradeInfo, sessionDownloadTask, "orderApp", "predl_", 1, "wlanidle", dVar);
            return;
        }
        dVar.ˋ(null);
    }

    public boolean ˎ() {
        return dss.ॱ().ˎ();
    }

    public boolean ˋ(SessionDownloadTask sessionDownloadTask) {
        return ((cdg) cdb.ॱ(cdg.class)).ॱॱ(sessionDownloadTask.ˊˊ());
    }

    public int ˊ(@NonNull SessionDownloadTask sessionDownloadTask) {
        return 3;
    }

    public void ˊ(int i) {
        Logger.ˎ(this.ˊ, "end update dl:" + i);
        eds.ˏ(ContentManager.getInstant().getContext(), i);
    }

    public void ʼ() {
        Logger.ˎ(this.ˊ, "on end download");
        eds.ˏ();
    }

    public void ˊ(List<String> list) {
        eds.ˋ(ContentManager.getInstant().getContext(), list);
    }
}
